// new表格
function Dialog(dialogStructure, dialogName,container) {
	 this.dialogStructure=dialogStructure;
	 this.dialogName=dialogName;
	 this.container=container;
	 this.dialog=null;
     this.iframe=null;
     this.iframeName=dialogName+"_iframe";
	 this.init();
}
Dialog.prototype = {
	init:function(){
		if(this.dialogStructure){
			this.display=this.dialogStructure.display;
			this.buttons=this.dialogStructure.buttons;
			this.width=this.dialogStructure.width||document.body.scrollWidth-50;
			this.height=this.dialogStructure.height||document.body.scrollHeight;
			this.isClose=this.dialogStructure.isClose;
			this.action =this.dialogStructure.action;
			this.title=this.dialogStructure.title;
            this.isIframe=this.dialogStructure.isIframe||"true";
		}
		if(!document.getElementById(this.dialogName+"_")){
			 $('<div id="'+this.dialogName+'_" title="'+this.title+'" style="overflow: auto"></div>' ).appendTo($("body"));
		}
		this.container=$("#"+this.dialogName+"_");
	},
	open : function() {
		var that=this;
        var createDialog=function(){
            var btObj={};
            var action=function(b){
                return function(){
                    if(b.action){
                        b.action(that);
                    }
                    if(b.isClose){
                        that.close(that);
                    }
                };
            };
            for(var i=0;i<that.buttons.length;i++){
                var btn=that.buttons[i];
                btObj[btn.text]=action(btn);
            }
            that.dialog=that.container.dialog({
                autoOpen: true,
                height: that.height,
                width: that.width,
                modal: true,
                buttons: btObj,
                close: function() {
                    that.close();
                }
            });
        };
		if(this.display){
			if(typeof(that.display)=="string"){
                if(that.isIframe=="true")
                {
                    var frm= $('<iframe id="'+that.iframeName+'" src="'+that.display+'" frameborder="0"  width="100%" height="100%" ></iframe>');
                   frm.appendTo(that.container);
                   that.iframe=frm[0];
                   createDialog();
                }
                else
                {
                    that.container.load(that.display,function(responseTxt,statusTxt,xhr){
                        if(statusTxt=="success"){
                            createDialog();
                        }
                    });
                }
			}
		};
		return that.dialog;
	},
	getIframe:function(){
		return document.getElementById(this.iframeName).contentWindow;
	},
	confirm:function(content,fun,title){
		var that=this;
		that.container.html(content);
		var t=title||"确认";
		that.container.attr("title",t);
		this.dialog=that.container.dialog({
			  resizable: false,
			  height: 260,
		      width: 450,
			  modal: true,
			  buttons: {"确认":function(){
				  fun();
				  that.close();
			  },"关闭":function(){
				  that.close();
			  }}
		});	 	
	},
	alert:function(content,title){
		var that=this;
		that.container.html(content);
		var t=title||"提示";
		that.container.attr("title",t);
		this.dialog=that.container.dialog({
			 resizable: false,
			  height: 260,
		      width: 450,
			  modal: true,
			  buttons: {"关闭":function(){
				 that.close();
			  }}
		});	 	
	},
	close:function(){
		var that=this;
		this.dialog.dialog( "close" );
//		this.dialog.dialog( "destroy" );
		setTimeout(function(){
			var p=$("#"+that.dialogName+"_");
			p.innerHTML="";
			p.remove();
		},50);
		//$("#"+this.dialogName+"_").parent().remove();
		//this.dialog=null;
	},
	getAction:function(){
		return this.action;
	}
}; 
jyDialog = function(dialogStructure) {
	var mName="dialog"+(new Date()).getTime();
   	return new Dialog(dialogStructure,mName);
  };    

